package com.yisu.expressway.application;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;


/**
 * Activity生命周期回调实现
 * @author hugh
 * @since 1.2.2
 * @date 16/4/18
 */
public class ExAppLifecycle implements Application.ActivityLifecycleCallbacks {

    private int mForegroundActivities;
    private boolean mIsLaunch = false;
    private boolean mIsConfigurationChange = false;

    private static ExAppLifecycle sInstance;

    private ExAppLifecycle() {
    }

    public boolean isForegroud(){
        return mForegroundActivities != 0;
    }

    public synchronized static ExAppLifecycle getIntance() {
        if (sInstance == null) {
            sInstance = new ExAppLifecycle();
        }

        return sInstance;
    }

    /**
     * 设置app是否启动标志为
     * @param isLaunch
     */
    public void setIsLaunch(boolean isLaunch) {
        mIsLaunch = isLaunch;
    }

    public boolean isLaunch() {
        return mIsLaunch;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        mForegroundActivities++;
        if (mIsLaunch && mForegroundActivities == 1 && !mIsConfigurationChange) {
//            appDidEnterForeground();
        }
        mIsLaunch = true;
        mIsConfigurationChange = false;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        mForegroundActivities--;
        mIsConfigurationChange = activity.isChangingConfigurations();
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

}
